﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponents.Design.Attributes;
using System.ComponentModel;
using ProgNetComponents.NamedObjects;
using ProgNetComponents.Design.Editors;
using System.Drawing.Design;
using ProgNetDxComponents.Design.Editors;
using ProgNetComponents;
using ProgNetDxComponents.Design.Designers;

namespace ProgNetDxComponents.ExtenderProviders.Browsers
{

    public class Browser : ProgNetComponents.Components.CustomTypeDescriptorObject, ICloneable, INamedObject
    {
        private BrowserColumnCollection _Columns = new BrowserColumnCollection();

        [Category(Categories.Display)]
        public string ConfigGroup
        {
            get;
            set;
        }

        [Category(Categories.Display)]
        public BrowserDisplayType DisplayType
        {
            get;
            set;
        }

        [Category(Categories.Display)]
        public string Caption
        {
            get;
            set;
        }

        [Category(Categories.Data)]
        public string Name
        {
            get;
            set;
        }

        [DynamicHiddenByProperty("Static", true),
        Category(Categories.Data)]
        public string SelectFilter
        {
            get;
            set;
        }

        [RefreshProperties(System.ComponentModel.RefreshProperties.All),
        Category(Categories.Data)]
        public bool Static
        {
            get;
            set;
        }

        [DynamicBrowsableByProperty("Static", true),
        Category(Categories.Data)]
        public bool GetDataOnce
        {
            get;
            set;
        }

        [DynamicBrowsableByProperty("GetDataOnce", false),
        DynamicBrowsableByProperty("Static", true),
        Category(Categories.Data)]
        public BrowserRefreshDataType RefreshType
        {
            get;
            set;
        }
        
        [Category(Categories.Data),
        Editor(typeof(BrowserColumnEditor), typeof(UITypeEditor))]
        public string DisplayMember
        {
            get;
            set;
        }

        [Category(Categories.Data),
        Editor(typeof(BrowserColumnEditor), typeof(UITypeEditor))]
        public string ValueMember
        {
            get;
            set;
        }

        [Category(Categories.Data),
        DynamicHiddenByProperty("Static", true),
        TypeConverter(typeof(ProgNetComponents.Design.TypeConverters.StringArrayTypeConverter))]
        public string[] Tables
        {
            get;
            set;
        }

        [Category(Categories.Data),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public BrowserColumnCollection Columns
        {
            get { return _Columns; }
        }
        [Category(Categories.Display),
        RefreshProperties(System.ComponentModel.RefreshProperties.All)]
        public bool IsTreeBrowser
        {
            get;
            set;
        }

        [Category(Categories.Data),
        DynamicBrowsableByProperty("IsTreeBrowser", true),
        Editor(typeof(BrowserColumnEditor), typeof(UITypeEditor))]
        public string IdColumn
        {
            get;
            set;
        }

        [Category(Categories.Data),
        DynamicBrowsableByProperty("IsTreeBrowser", true),
        Editor(typeof(BrowserColumnEditor), typeof(UITypeEditor))]
        public string ChildColumn
        {
            get;
            set;
        }

        #region ICloneable Members

        public object Clone()
        {
            Browser b = new Browser();
            b.Name = Name;
            b.SelectFilter = SelectFilter;
            b.Static = Static;
            b.DisplayMember = DisplayMember;
            b.ValueMember = ValueMember;
            b.Caption = Caption;
            b.RefreshType = RefreshType;
            b.ConfigGroup = ConfigGroup;
            b.DisplayType = DisplayType;
            b.GetDataOnce = GetDataOnce;
            b.Tables = this.Tables;
            foreach (BrowserColumn c in Columns)
            {
                b.Columns.Add(c.Clone() as BrowserColumn);
            }
            return b;
        }

        #endregion

        private BrowserForm _Form;
        private BrowserForm Form
        {
            get
            {
                if (_Form == null)
                    _Form = new BrowserForm();
                return _Form;
            }
        }
       
        public ObjectDialogResult ShowDialog(object value = null, bool allowNew = false, string additionalFilter = null)
        {
            Form.InitForm(this, allowNew, value);
            Form.DisplayFilter = additionalFilter;
            return Form.ShowDlg();
        }
    }

    [Editor(typeof(BrowserCollectionEditor), typeof(UITypeEditor))]
    public class BrowserCollection : NamedCollectionBase<Browser>
    {
        protected override bool AllowDuplicates
        {
            get
            {
                return true;
            }
        }
    }

    [ToolboxItem(true),
    Designer(typeof(BrowserContainerDesigner))]
    public class BrowserContainer : Component
    {
        private BrowserCollection _Items = new BrowserCollection();
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public BrowserCollection Items
        {
            get { return _Items; }
        }
    }
    
}
